package com.seu.vcampus.service;

import com.seu.vcampus.dao.BookDao;
import com.seu.vcampus.dao.BorrowRecordDao;
import com.seu.vcampus.vo.Book;
import com.seu.vcampus.vo.BorrowRecord;
import java.sql.Connection;
import java.util.List;

/**
 * 图书馆服务类
 * 处理图书馆相关的业务逻辑
 * 
 * @author 虚拟校园开发团队
 * @since 1.0.0
 */
public class LibraryService {
    private final BookDao bookDao;
    private final BorrowRecordDao borrowRecordDao;
    
    public LibraryService(Connection connection) {
        this.bookDao = new BookDao(connection);
        this.borrowRecordDao = new BorrowRecordDao(connection);
    }
    
    /**
     * 获取所有图书
     */
    public List<Book> getAllBooks() {
        return bookDao.getAllBooks();
    }
    
    /**
     * 搜索图书
     */
    public List<Book> searchBooks(String keyword) {
        return bookDao.searchBooks(keyword);
    }
    
    /**
     * 根据ID获取图书
     */
    public Book getBookById(String bookId) {
        return bookDao.getBookById(bookId);
    }
    
    /**
     * 借阅图书
     */
    public boolean borrowBook(String userId, String bookId) {
        // 检查图书是否存在
        Book book = bookDao.getBookById(bookId);
        if (book == null) {
            return false;
        }
        
        // 检查库存
        if (book.getStock() <= 0) {
            return false;
        }
        
        // 检查用户是否已经借阅过这本书
        if (borrowRecordDao.hasBorrowedBook(userId, bookId)) {
            return false;
        }
        
        // 检查用户借阅数量限制（最多5本）
        if (borrowRecordDao.getBorrowCount(userId) >= 5) {
            return false;
        }
        
        // 创建借阅记录
        boolean borrowSuccess = borrowRecordDao.createBorrowRecord(userId, bookId);
        if (!borrowSuccess) {
            return false;
        }
        
        // 更新库存
        return bookDao.updateBookStock(bookId, book.getStock() - 1);
    }
    
    /**
     * 归还图书
     */
    public boolean returnBook(int recordId, String bookId) {
        // 归还图书
        boolean returnSuccess = borrowRecordDao.returnBook(recordId);
        if (!returnSuccess) {
            return false;
        }
        
        // 获取当前图书信息
        Book book = bookDao.getBookById(bookId);
        if (book == null) {
            return false;
        }
        
        // 更新库存
        return bookDao.updateBookStock(bookId, book.getStock() + 1);
    }
    
    /**
     * 获取用户的借阅记录
     */
    public List<BorrowRecord> getBorrowRecords(String userId) {
        return borrowRecordDao.getBorrowRecordsByUserId(userId);
    }
    
    /**
     * 获取所有借阅记录（管理员专用）
     */
    public List<BorrowRecord> getAllBorrowRecords() {
        return borrowRecordDao.getAllBorrowRecords();
    }
    
    /**
     * 获取用户当前借阅中的记录
     */
    public List<BorrowRecord> getCurrentBorrowRecords(String userId) {
        return borrowRecordDao.getCurrentBorrowRecords(userId);
    }
    
    /**
     * 添加图书（管理员专用）
     */
    public boolean addBook(Book book) {
        return bookDao.addBook(book);
    }
    
    /**
     * 更新图书信息（管理员专用）
     */
    public boolean updateBook(Book book) {
        return bookDao.updateBook(book);
    }
    
    /**
     * 删除图书（管理员专用）
     */
    public boolean deleteBook(String bookId) {
        // 检查图书是否存在
        Book book = bookDao.getBookById(bookId);
        if (book == null) {
            return false;
        }
        
        // 检查是否有未归还的借阅记录
        if (borrowRecordDao.hasActiveBorrowRecords(bookId)) {
            return false;
        }
        
        return bookDao.deleteBook(bookId);
    }
    
    /**
     * 检查图书是否被借阅
     */
    public boolean isBookBorrowed(String bookId) {
        return borrowRecordDao.hasActiveBorrowRecords(bookId);
    }
}